﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using SmugMugModel;

namespace SmugMugPhotoShow
{
    public partial class PhotoFrame : Form
    {
        private Site mySite = new Site();
        private MyUser CurrentUser = null;
        private bool IsAlreadyLogged = false;
        //private bool UserAlreadyLogged = false;
        private List<SmugMugModel.Image> CurrentUserImagesList = null;
        private Random myRandom = new Random((int)DateTime.Now.Ticks);
        private WebClient myWebClient = new WebClient();

        public PhotoFrame()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Log in to SmugMug using the LogInWithHash, if you have the UserId and PasswordHash stored in the registries
        /// </summary>
        /// <returns></returns>
        private bool SmugMugLogInWithHash()
        {
            object UserId = Application.UserAppDataRegistry.GetValue("UserId");
            object Password = Application.UserAppDataRegistry.GetValue("Password");
            if ((UserId == null) || string.IsNullOrEmpty((string)Password))
                return false;
            try
            {
                CurrentUser = mySite.Login((int)UserId, (string)Password);
            }
            catch (SmugMugException sme)
            {
                MessageBox.Show(sme.Message);
                CurrentUser = null;
                return false;
            }
            IsAlreadyLogged = true;
            return true;
        }

        /// <summary>
        /// Log in to SmugMug using a UserName and Password
        /// </summary>
        private void SmugMugLogIn()
        {
            LogIn li = new LogIn();
            DialogResult dr;
            // Try to login as long as there is no valid SmugMug user (the user didn't provide valid user name and password), and the Ok button was pressed
            do
            {
                dr = li.ShowDialog(this);
            }
            while ((dr == DialogResult.OK) && (li.user == null));

            // The log in was successful or/and the Cancel button was pushed
            // If the log in was succesful, it returns a user which you have to  make the current user, for the appropriate galleries to be loaded
            if (li.user != null)
            {
                CurrentUser = li.user;
                IsAlreadyLogged = true;
            }
            // If the Cancel button was pushed, if there was a user already logged in, just reload his galleries, if not, exit the app as you have no user logged in
            else
            {
                if (IsAlreadyLogged == true)
                {
                    MessageBox.Show("The user wasn't changed");
                    //UserAlreadyLogged = true;
                }
                else
                {
                    MessageBox.Show("The login wasn't successful! The plug-in will now close!");
                    Close();
                }
            }
        }

        /// <summary>
        /// Get all the albums for the user that's logged in, from SmugMug
        /// </summary>
        /// <returns></returns>
        private List<SmugMugModel.Album> GetAlbumsForCurrentUser()
        {
            List<Album> CurrentUserAlbumsList = null;
            if (CurrentUser != null)
            {
                try
                {
                    CurrentUserAlbumsList = CurrentUser.GetAlbums();
                }
                catch (SmugMugException sme)
                {
                    MessageBox.Show(sme.Message);
                }
            }
            return CurrentUserAlbumsList;
        }
 
        /// <summary>
        /// Get all the images for the user that's logged in, from SmugMug
        /// </summary>
        /// <returns></returns>
        private List<SmugMugModel.Image> GetImagesForCurrentUser()
        {
            List<SmugMugModel.Album> CurrentUserAlbumsList = GetAlbumsForCurrentUser();
            List<SmugMugModel.Image> CurrentUserImagesList = null;
            foreach (var album in CurrentUserAlbumsList)
            {
                List<SmugMugModel.Image> temp = null;
                try
                {
                    temp = album.GetImages(true);
                }
                catch (SmugMugException sme)
                {
                    MessageBox.Show(sme.Message);
                }
                if (CurrentUserImagesList == null)
                    CurrentUserImagesList = temp;
                else
                    CurrentUserImagesList.AddRange(temp);
                //myWebClient.DownloadFile(CurrentUserImagesList[0].TinyURL, 
            }
            return CurrentUserImagesList;
        }

        /// <summary>
        /// Display a random picture
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private void displayImagesEvent(object source, EventArgs e)
        {
            int index = myRandom.Next(1, CurrentUserImagesList.Count);
            RandomPhotoPctrBx.ImageLocation = CurrentUserImagesList.ElementAt(index).SmallURL;
        }

        /// <summary>
        /// Loads the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PhotoFrame_Load(object sender, EventArgs e)
        {
            // Log in to SmugMug using the UserId and PasswordHash if stored in the registries, otherwise ask for user name and password until you get a good one or a cancel
            if (SmugMugLogInWithHash() == false)
            {
                SmugMugLogIn();
            }

            CurrentUserImagesList = GetImagesForCurrentUser();
            Timer t = new Timer();
            t.Interval = 5000;
            t.Enabled = true;
            t.Tick += new EventHandler(displayImagesEvent);
        }

        private void OptionsBttn_Click(object sender, EventArgs e)
        {
            Options o = new Options();
            o.ShowDialog(this);
            if (o.DialogResult == DialogResult.OK)
            {

            }
        }
    }
}
